﻿Imports lbGlobalcom
Public Class fAdminPrepacks
    Dim daPrep As dsInventariosTableAdapters.INV_PREPACKTableAdapter
    Dim daInveDtalle As dsInventariosTableAdapters.INV_DETALLETableAdapter
    Dim lNumAnte As String
    Private Sub fAdminPrepacks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            daPrep = New dsInventariosTableAdapters.INV_PREPACKTableAdapter
            daInveDtalle = New dsInventariosTableAdapters.INV_DETALLETableAdapter
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscarPrepack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarPrepack.Click
        Try
            Dim flPre As New fListaPrepack
            'flPre.MdiParent = Me
            'flPre.WindowState = FormWindowState.Maximized
            If flPre.ShowDialog = Windows.Forms.DialogResult.OK Then
                daPrep.FillByIdPrepackConsec(DsInventarios.INV_PREPACK, flPre.mIdPrepack, flPre.mConsecutivo)
                INV_PREPACKDataGridView.Focus()
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnGuardarPrepack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardarPrepack.Click
        Try
            Validate()
            INV_PREPACKBindingSource.EndEdit()
            daPrep.Update(DsInventarios.INV_PREPACK)
            MessageBox.Show("Se salvarón registros con exito", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub INV_PREPACKDataGridView_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles INV_PREPACKDataGridView.CellValidated
        Try
            Select Case e.ColumnIndex
                Case 10
                    INV_PREPACKBindingSource.EndEdit()
                    daPrep.Update(DsInventarios.INV_PREPACK)
                    'MessageBox.Show("Se salvarón registros con exito", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Select
        Catch ex As Exception

        End Try
    End Sub



    
    Private Sub INV_PREPACKDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles INV_PREPACKDataGridView.CellValidating
        Try
            Select Case e.ColumnIndex
                Case 10
                    If INV_PREPACKDataGridView.CurrentRow.Cells("TIPOSUBPRODH").Value <> "0" Then
                        If e.FormattedValue <> INV_PREPACKDataGridView.CurrentRow.Cells("NUMEROREGISTROH").Value Then
                            If daInveDtalle.GetImeiIccidValido(e.FormattedValue) = 1 Then
                                If daPrep.GetImeiAndIccidValido(INV_PREPACKDataGridView.CurrentRow.Cells("IDPRODUCTOH").Value, e.FormattedValue) = 0 Then
                                    'daPrep.Update(DsInventarios.INV_PREPACK)
                                    'MessageBox.Show("Se salvarón registros con exito", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                    'MessageBox.Show("SE EINGRESO")
                                ElseIf daPrep.GetIccidGuardado(INV_PREPACKDataGridView.CurrentRow.Cells("IDPREPACK").Value, INV_PREPACKDataGridView.CurrentRow.Cells("CONSECPREPACK").Value, INV_PREPACKDataGridView.CurrentRow.Cells("IDPRODUCTOH").Value, e.FormattedValue) = 1 Then
                                    MessageBox.Show("Iccid ya pertenece a otro Prepack", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                    e.Cancel = True
                                Else
                                    'daPrep.Update(DsInventarios.INV_PREPACK)
                                    'MessageBox.Show("Se salvarón registros con exito", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                End If
                            Else
                                'INV_PREPACKDataGridView.CurrentRow.Cells("NUMEROREGISTROH").Value
                                MessageBox.Show("Iccid no valido", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                e.Cancel = True
                            End If
                        End If
                    ElseIf e.FormattedValue <> "" Then
                        MessageBox.Show("No se puede Ingresar Iccid", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        e.Cancel = True
                    End If

                    'INV_PREPACKDataGridView.Focus()
            End Select
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnEliminarPrepack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarPrepack.Click
        Try
            If INV_PREPACKDataGridView.RowCount > 0 Then
                Dim cGetDatos As GetDatosPrepack = New GetDatosPrepack(INV_PREPACKDataGridView.Rows(INV_PREPACKDataGridView.RowCount - 1).Cells("IDPRODUCTOP").Value, INV_PREPACKDataGridView.Rows(INV_PREPACKDataGridView.RowCount - 1).Cells("TIPOSUBPROP").Value, INV_PREPACKDataGridView.Rows(INV_PREPACKDataGridView.RowCount - 1).Cells("NUMEROREGISTROP").Value)
                If cGetDatos.EstadoPrepack <> "V" Then
                    Dim cEliPre As New cEliminaPrepaks
                    cEliPre.EliminaPrepack(INV_PREPACKDataGridView.Rows(INV_PREPACKDataGridView.RowCount - 1).Cells("IDPREPACK").Value, INV_PREPACKDataGridView.Rows(INV_PREPACKDataGridView.RowCount - 1).Cells("CONSECPREPACK").Value)
                    DsInventarios.INV_PREPACK.Rows.Clear()
                    MessageBox.Show("Se elimino el prepack con exito", "eliminaPrepack", MessageBoxButtons.OK)
                Else
                    MessageBox.Show("No se puede eliminar el prepack, Primero se debe eliminar la factura", "eliminaPrepack", MessageBoxButtons.OK)
                End If
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class